
package com.base.sms.service.impl;

import com.base.common.ConfigConstants;
import com.base.common.enums.AppType;
import com.base.common.enums.SmsCategories;
import com.base.common.enums.SmsReportStatus;
import com.base.common.enums.SmsType;
import com.base.common.json.JsonUtil;
import com.base.common.persistent.dao.*;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.Sms;
import com.base.common.persistent.model.SmsReport;
import com.base.common.persistent.model.Terminal;
import com.base.common.utils.IDGenerator;
import com.base.common.utils.PushUtils;
import com.base.sms.service.SmsService;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("smsService")
public class SmsServiceImpl implements SmsService {

	@Resource
	private EmployeeCompoundsMapper employeeCompoundsMapper;
	
	@Resource
	private EmployeeMapper employeeMapper;
	
	@Resource
	private SmsMapper smsMapper;
	
	@Resource
	private SmsReportMapper smsReportMapper;
	
	@Resource
	private TerminalMapper terminalMapper;

	/**
	 * 添加短消息
	 * 
	 * @param categories 消息类别
	 * @param title	消息标题
	 * @param context 消息内容
	 * @param resourceEmployeeId 消息发送者的员工id
	 * @param sendPhone 消息接收号码
	 * @param employeeId 消息接收员工小区中间表id
	 * @param userId 消息接收业主id
	 */
	@Transactional
	public Sms addSms(int categories, String title, String context, int resourceEmployeeId, String sendPhone, int employeeId, int userId) {
		Sms sms = new Sms();
		sms.setCategories(categories);
		sms.setTitle(title);
		sms.setCreateTime(new Date());
		String uuid = IDGenerator.getUUID().replace("-", "");
		sms.setUuid(uuid.trim());
		sms.setContext(context);
		smsMapper.addSms(sms);
		
		SmsReport smsReport = new SmsReport();
		smsReport.setSendTime(new Date());
		smsReport.setStatus(SmsReportStatus.NOTREAD.getValue());
		smsReport.setSms(smsMapper.querySmsByFuzzyCondition(uuid.trim(), 0, 0, 0, 0, 0, -2).get(0));
		smsReportMapper.addSmsReport(smsReport);
		return sms;
	}

	@Override
	@Transactional
	public int deleteSms(String smsReportIds) {
		try {
			for(String reportId : smsReportIds.split(",")) {
				smsReportMapper.updateReceiveVisiblenessById(1, Integer.valueOf(reportId.trim()));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	@Override
	public List<Sms> getSmsByFuzzyCondition(int bid, int categories, String code, int status,
			int page) {
		String uuid = null;
		List<Sms> smss = new ArrayList<Sms>();
		Terminal terminal = terminalMapper.queryRegistedTerminalByCodeAndBid(code, bid);
		smss = smsMapper.querySmsByFuzzyCondition(uuid, categories, terminal.getId(),  SmsType.MAIN.getValue(), 
				(page - 1) * 15, Integer.parseInt(ConfigConstants.INFOS_LIST_NUM), status);
		return smss;
	}

	@Override
	public int getSmsCountsByFuzzyCondition(int bid, int categories, String code, int status) {
		String uuid = null;
		Terminal terminal = terminalMapper.queryRegistedTerminalByCodeAndBid(code, bid);
		return smsMapper.querySmsCountsByFuzzyCondition(uuid, categories,  terminal.getId(), SmsType.MAIN.getValue(), status);
	}

	@Override
	public int replySms(int resourceEmployeeId, int smsId, String mess) {
		
		return 0;
	}
	
	// TODO
	@Override
	public int sendSmsToOther(List<Integer> employeeIds,
			String mess) throws Exception {
		// 获取session, 查找员工信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		List<Employee> employees = (List<Employee>) session.get("users");
		
		for(int employeeId : employeeIds) {
			Employee employee = employeeMapper.queryEmployeeById(employeeId);
			Sms sms = addSms(SmsCategories.PERSONMESS.getValue(),
					SmsCategories.PERSONMESS.getName(), mess, employees.get(0).getId(), employee.getPhone(),
					employeeId, 0);
			
			// 判断员工的设备是ios还是android
			String clientId = null;
			if (employee.getTerminal() != null) {
				if (employee.getTerminal().getAndroidClientId() != null && employee.getTerminal().getAndroidClientId().trim() != "") {
					clientId = employee.getTerminal().getAndroidClientId();
				} else if (employee.getTerminal().getAppleClientId() != null && employee.getTerminal().getAppleClientId().trim() != "") {
					clientId = employee.getTerminal().getAppleClientId();
				}
					System.out.println(PushUtils.pushtoSingle(clientId,
							JsonUtil.dto2String(sms), AppType.YGJ.getValue()));
			} else {
					throw new Exception();

			}
				
			
			
//			int employeeCompoundsId = employeeCompoundsMapper.queryEmployeeCompoundsIdByEmployeeIdAndCompoundsId(employeeId, compoundsId);
			/*Sms sms = new Sms();
			sms.setCategories(SmsCategories.PERSONMESS.getValue());
			sms.setContext(mess);
			SmsReport smsReport = new SmsReport();
			smsReport.setEmployeeCompoundsId(employeeCompoundsId);
			smsReport.setSms(sms);*/
		}
		return 1;
	}
	
	/*@Test
	public void test() throws Exception {
		List<Integer> intergers = new ArrayList<Integer>();
		intergers.add(3);
		intergers.add(4);
		System.out.println(JsonUtil.dto2String(intergers));
	}*/

	@Override
	@Transactional
	public int updateReadTimeStatusByReportId(String[] reportIds) {
		try {
			for(String reportId : reportIds) {
				smsReportMapper.updateReadTimeStatusById(new Date(), SmsReportStatus.READ.getValue(), Integer.valueOf(reportId.trim()));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

}
